package com.ruoyi.location.repository;

import com.ruoyi.framework.config.PwdUtil;
import com.ruoyi.location.exception.LocationRuntimeException;
import com.ruoyi.location.repository.generator.entity.*;
import com.ruoyi.location.repository.generator.mapper.LocationPhoneLoginCodeMapper;
import com.ruoyi.location.repository.generator.mapper.LocationUserInfoMapper;
import com.ruoyi.location.repository.generator.mapper.SysUserGenMapper;
import com.ruoyi.location.domain.quickreport.util.RichNicknameGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Repository
public class LoginRepository {

    @Autowired
    private SysUserGenMapper sysUserGenMapper;
    @Autowired
    private LocationPhoneLoginCodeMapper locationPhoneLoginCodeMapper;
    @Autowired
    private LocationUserInfoMapper locationUserInfoMapper;

    public Optional<SysUserGen> getUserByPhoneNumber(Long phoneNumber) {
        SysUserGenExample example = new SysUserGenExample();
        example.createCriteria().andPhonenumberEqualTo(phoneNumber.toString());
        List<SysUserGen> users = sysUserGenMapper.selectByExample(example);
        if (users.size() > 1) {
            throw new LocationRuntimeException("一个手机号对应了多个账号, 请联系管理员: " + phoneNumber);
        }
        return users.isEmpty() ? Optional.empty() : Optional.of(users.get(0));
    }

    public void savePhoneLoginCode(Long phpneNum, int code) {
        LocationPhoneLoginCode plc = new LocationPhoneLoginCode();
        plc.setPhonenum(phpneNum);
        plc.setMsgCode(String.valueOf(code));
        locationPhoneLoginCodeMapper.insertSelective(plc);
    }

    public LocationPhoneLoginCode getPhoneLoginCodeByPhoneNum(Long phoneNum) {
        LocationPhoneLoginCodeExample e = new LocationPhoneLoginCodeExample();
        e.createCriteria().andPhonenumEqualTo(phoneNum);
        e.setOrderByClause("ctime DESC");

        List<LocationPhoneLoginCode> list = locationPhoneLoginCodeMapper.selectByExample(e);
        if (list.isEmpty()) {
            throw new LocationRuntimeException("手机号错误，不存在当前手机号" + phoneNum);
        }
        return list.get(0);
    }

    public SysUserGen insertIfNullSysUser(Long phoneNum) {
        SysUserGenExample e = new SysUserGenExample();
        e.createCriteria().andPhonenumberEqualTo(phoneNum.toString());
        List<SysUserGen> list = sysUserGenMapper.selectByExample(e);
        if (list.isEmpty()) {
            SysUserGen sysUser = new SysUserGen();
            sysUser.setLoginName(phoneNum.toString());
            sysUser.setPhonenumber(phoneNum.toString());
            sysUser.setUserName(phoneNum.toString());
            sysUser.setPassword(PwdUtil.encrypt(phoneNum.toString()));
            sysUser.setCreateTime(new Date());
            sysUser.setUpdateTime(new Date());
            sysUserGenMapper.insertSelective(sysUser);
            return sysUser;
        }
        return list.get(0);
    }

    public LocationUserInfo insertIfNullLocationUser(Long userId, Long phoneNum) {
        LocationUserInfo user = locationUserInfoMapper.selectByPrimaryKey(userId);
        if (user == null) {
            user = new LocationUserInfo();
            user.setUserId(userId);
            user.setPhoneNum(phoneNum);
            user.setNickName(RichNicknameGenerator.generateNickname());
            user.setAvatar("");
            locationUserInfoMapper.insertSelective(user);
        }
        return user;
    }

    public LocationUserInfo getUserInfo(Long userId) {
        LocationUserInfo user = locationUserInfoMapper.selectByPrimaryKey(userId);
        if (user == null) {
            throw new LocationRuntimeException("用户不存在");
        }
        return user;
    }

}
